package com.xq.chrome.web.service.impl;

import com.xq.chrome.web.dao.UserRepository;
import com.xq.chrome.web.entity.UserVo;
import com.xq.chrome.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MACHENIKE
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

  @Autowired
  UserRepository userDao;

  @Override
  public UserVo getUser(Integer id) {
    System.out.println(id);
    if (id != null && id > 0) {
      return userDao.getOne(id);
    }
    return null;
  }

  @Override
  public boolean addUser(UserVo user) {
    System.out.println(user);
    if (user == null) {
      return false;
    }
    List<UserVo> rs = userDao.findByAccount(user.getAccount());
    if (rs != null && rs.size() != 0) {
      System.out.println("帐号已经存在!");
      return false;
    }
    user = userDao.save(user);
    return true;
  }


  /**
   * 不带排序的简单分页查询.
   *
   * @param page
   * @param size
   * @return
   */
  public List<UserVo> pageQuery(int page, int size) {
    PageRequest pageRequest = PageRequest.of(page, size);
    Page<UserVo> result = userDao.findAll(pageRequest);
    return result.getContent();
  }

}
